package com.tt.thread;

import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;

import java.io.*;

/**
 * Tail 命令线程
 * 2022/3/7 - 11:37
 */
public class TailLogThread implements Runnable
{

    private BufferedReader reader;

    /**
     * WebSocket 的session
     */
    private WebSocketSession session;

    /**
     * 构造方法赋值
     * @param in
     */
    public TailLogThread(InputStream in,WebSocketSession session) throws FileNotFoundException
    {
        this.reader = new BufferedReader(new InputStreamReader(in));
//        this.reader = new BufferedReader(new FileReader("F:\\SpringMVP\\tt-shop\\shop-system\\logs\\tt-shop-system.log"));
        this.session = session;
    }

    @Override
    public void run()
    {
        String line;
        try
        {
            while((line = reader.readLine()) != null)
            {

                // 将实时日志通过WebSocket发送给客户端，给每一行添加一个HTML换行
                session.sendMessage(new TextMessage(line + "<br>"));
            }
        } catch (IOException e)
        {
            e.printStackTrace();
        }
    }
}
